package com.yvdelx.cyclowomen.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.yvdelx.R;
import com.yvdelx.cyclowomen.activity.listener.DayActivityListener;
import com.yvdelx.cyclowomen.mediation.CycleManagement;
import com.yvdelx.cyclowomen.object.Day;
import com.yvdelx.cyclowomen.utility.ActivityUtility;
import com.yvdelx.cyclowomen.utility.DateUtility;
import com.yvdelx.cyclowomen.utility.LogUtility;
import com.yvdelx.cyclowomen.utility.StringUtility;
import com.yvdelx.cyclowomen.utility.ViewUtility;

import java.util.ArrayList;
import java.util.List;

import static com.yvdelx.cyclowomen.utility.StringUtility.CST_BUNDLE;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_DATE;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_IS_MODIFICATION;

/**
 * Day Consultation activity display day data
 */
public class DayConsultationActivity extends Activity {


    // IDs from the R.id
    private final static int CST_ID_TEMPERATURE_RESULT = R.id.TemperatureValueTextResult;
    private final static int CST_ID_SENSATION_RESULT = R.id.SensationValueTextResult;
    private final static int CST_ID_NATURE_RESULT = R.id.NatureTextResult;
    private final static int CST_ID_COMMENT_RESULT = R.id.CommentValueTextResult;
    private final static int CST_ID_RATING_BAR = R.id.QuantityRatingBar;
    private final static int CST_ID_POK_CHECK_BOX = R.id.pok_check_box;

    private Day day;

    private static List<Activity> listConsultationActivities = new ArrayList<Activity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_consultation);

        ActivityUtility.addActivity(this);

        listConsultationActivities.add(this);

        // Get bundle from the previous activity
        Bundle bundleCalendarActivity = this.getIntent()
                .getBundleExtra(StringUtility.CST_BUNDLE);

        if(bundleCalendarActivity == null){
            this.finish();
        } else {
            // Get date from the previous activity in the bundle
            Long dateLong = bundleCalendarActivity.getLong(StringUtility.CST_DATE);

            // Call to CycleManagement to get the day with the date
            this.day = CycleManagement.getDay(DateUtility.getDateFromLong(dateLong));

            // Put data in the view
            ViewUtility.setTextInTextView(this.findViewById(CST_ID_TEMPERATURE_RESULT),
                    String.valueOf(day.getTemperature())
                            + " "
                            + getResources().getString(R.string.degree));
            ViewUtility.setTextInTextView(this.findViewById(CST_ID_SENSATION_RESULT),
                    ViewUtility.getSensation(day));
            ViewUtility.setRatingOnRatingBar(this.findViewById(CST_ID_RATING_BAR), day.getQuantity());
            ViewUtility.setTextInTextView(this.findViewById(CST_ID_NATURE_RESULT),
                    ViewUtility.getNature(day));
            String dayComment = day.getComment();
            if(dayComment == null || dayComment.isEmpty()){
                dayComment = this.getResources().getString(R.string.no_comment);
            }
            ViewUtility.setTextInTextView(this.findViewById(CST_ID_COMMENT_RESULT), dayComment);
            ViewUtility.setValueCheckBox(this.findViewById(CST_ID_POK_CHECK_BOX),
                    day.getIsNominalCondition());


            ActivityUtility.setNewTitle(this, day.getDate());
        }

        // Set the lister for buttons next and previous day
        DayActivityListener buttonListener
                = new DayActivityListener(this.day,this);
        ViewUtility.setViewOnClickListener(this.findViewById(R.id.NextDayButton),buttonListener);
        ViewUtility.setViewOnClickListener(this.findViewById(R.id.PreviousDayButton),
                buttonListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.day_consultation, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        // Close all day consultation activities before finish this activity
        listConsultationActivities.remove(this);
        ActivityUtility.closeActivities(listConsultationActivities);
        listConsultationActivities.clear();
        this.finish();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Modify day selected
        if(item.getItemId() == R.id.modifyday){
            Intent intent = new Intent(this.getApplicationContext(), DayModificationActivity.class);
            Bundle bundle = new Bundle();
            bundle.putLong(CST_DATE, DateUtility.getLongFromDate(this.day.getDate()));
            bundle.putBoolean(CST_IS_MODIFICATION,true);
            intent.putExtra(CST_BUNDLE, bundle);
            this.startActivity(intent);
        } else if(item.getItemId() == R.id.deleteday){ // Delete day selected
            try {
                CycleManagement.delete(this.day);
                ActivityUtility.closeActivity(this);
            } catch (Exception e) {
                LogUtility.error(this.getClass(),e.getMessage(),e);
                //noinspection ConstantConditions
                Toast.makeText(this.getApplicationContext(),
                        R.string.deletedayerror,Toast.LENGTH_LONG).show();
            }
        }
        return super.onOptionsItemSelected(item);
    }
}
